package game.targets.entity.world.arrow;

import game.targets.entity.CollisionEntity;
import game.targets.entity.world.Wall;
import game.targets.observer.Event;
import game.targets.observer.Subject;

import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.math.Vector3;

public class Arrow extends CollisionEntity {

	public static float MAX_VELOCITY = 50;

	private float alpha;
	private ArrowState state;
	private float sideAngle;

	public static float DELETE_TIME = 2;

	public Arrow(Vector3 pos, Subject subject) {
		super(pos, new Vector3(0, 0, 0), new Vector3(0, 0, 0), 1f, new Vector3(1.8f, 0.1f, 0.1f), false, false, ARROW_MODEL, subject);
		alpha = 1;
		this.setScale(0.2f);
		state = new ArrowNormal(this);
	}

	@Override
	public void update(float delta) {
		super.update(delta);
		state.update(delta);
	}

	@Override
	public void render(ModelBatch batch, Environment environment) {
		graphics.renderFade(this, batch, environment, alpha);
	}

	@Override
	public void onCollide(CollisionEntity a) {
		if (!(state instanceof ArrowHit) && a instanceof Wall) {
			subject.notify(this, Event.EVENT_ARROW_HIT_WALL);
		}
		this.getBody().setLinearVelocity(new Vector3(0, 0, 0));
		this.getBody().setActivationState(0);

		changeState(new ArrowHit(this));

	}

	public void rotate(Vector3 axis, float angle) {
		sideAngle = angle;
		graphics.rotate(axis, angle);
	}

	// charge is a value from 0 to 1
	public void fire(Vector3 direction, float charge) {
		Vector3 dir = new Vector3(direction);
		dir.nor();
		dir.scl(MAX_VELOCITY);
		physics.getBody().setLinearVelocity(dir);
		changeState(new ArrowDown(this));
	}

	public void changeState(ArrowState st) {
		state = st;
	}

	public float getAlpha() {
		return alpha;
	}

	public void setAlpha(float alpha) {
		this.alpha = alpha;
	}

	public float getSideAngle() {
		return sideAngle;
	}

	public void setSideAngle(float sideAngle) {
		this.sideAngle = sideAngle;
	}

}
